package com.xjk.shiro.realms;

import com.xjk.shiro.entity.Permission;
import com.xjk.shiro.entity.Role;
import com.xjk.shiro.entity.User;
import com.xjk.shiro.entity.UserRoles;
import com.xjk.shiro.service.PermissionService;
import com.xjk.shiro.service.UserService;
import com.xjk.shiro.utils.MyAppContextComponent;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author: XJK
 */
public class UserRealm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String primaryPrincipal = (String) principalCollection.getPrimaryPrincipal();
        // 通过ApplicationContext获取UserService
        UserService userService = (UserService) MyAppContextComponent.getAppContext().getBean("UserService");
        PermissionService permissionService = (PermissionService) MyAppContextComponent.getAppContext().getBean("PermissionService");
        UserRoles userRoles = userService.findRoleByUsername(primaryPrincipal);
        if(!userRoles.getRoleList().isEmpty()){
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            for (Role role : userRoles.getRoleList()) {
                simpleAuthorizationInfo.addRole(role.getName());


                //权限信息
                List<Permission> permissionByRoleId = permissionService.findPermissionByRoleId(role.getId());
                if(!(permissionByRoleId.isEmpty())&&permissionByRoleId.get(0)!=null){
                    for (Permission permission : permissionByRoleId) {
                        simpleAuthorizationInfo.addStringPermission(permission.getPValue());
                    }
                }
            }
            return simpleAuthorizationInfo;
        }
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String p = (String) token.getPrincipal();
        // 通过ApplicationContext获取UserService
        UserService userService = (UserService) MyAppContextComponent.getAppContext().getBean("UserService");
        User userByUsername = userService.findByUsername(p);
        if (!ObjectUtils.isEmpty(userByUsername)) {
            return new SimpleAuthenticationInfo(userByUsername.getUsername(),
                    userByUsername.getPassword(),
                    ByteSource.Util.bytes(userByUsername.getSalt()),
                    this.getName());
        }
        return null;
    }
}
